//
//  HomeClassifyView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/4.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "HomeClassifyView.h"
#import "HomeClassifyItemView.h"

@interface HomeClassifyView ()
@property (nonatomic, strong) UIView *bannerView;
@property (nonatomic, strong) UIImageView *newImageView;
@property (nonatomic, strong) UILabel *newLabel;
@property (nonatomic, strong) CCPScrollView *ccpView;
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation HomeClassifyView

+ (CGFloat)getHeightWithArr:(NSMutableArray *)arr {
    return 82 * ((arr.count + 3) / 4) + 40;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configure];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configure];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.scrollView.frame = CGRectMake(0, 0, self.width, self.height - 30);
    
    self.bannerView.frame = CGRectMake(10, self.height - 5 - 30 - 5, self.width - 20, 30);
    self.newImageView.frame = CGRectMake(12, 7, 28, 16);
    self.newLabel.frame = CGRectMake(self.newImageView.right + 1, 0, 35, self.bannerView.height);
}

#pragma mark - Method
- (void)configure {
    self.backgroundColor = [UIColor grayF9Color];
    [self addSubview:self.scrollView];
    [self addSubview:self.bannerView];
    [self.bannerView addSubview:self.newImageView];
    [self.bannerView addSubview:self.newLabel];
    
    self.ccpView = [[CCPScrollView alloc] initWithFrame:CGRectMake(88, 0, ScreenWidth - 20 - 88 - 20, 30)];
    
    //self.ccpView.titleArray = [NSArray arrayWithObjects:@"iPhone X 上线无Home键 刘海手机你怎么看？",@"2017年还有10天就要过去了,2018年您准备好了吗?",@"今年双11您预算了几个月的工资？",@"吃鸡如日中天，你更看好哪个公司的吃鸡手游？", nil];
    
    self.ccpView.textFont = TextPingFangFontSize(12);
    self.ccpView.titleColor = [UIColor blackTextColor];
    [self.ccpView clickTitleLabel:^(NSInteger index,NSString *titleString) {
        NSLog(@"%ld-----%@",index,titleString);
    }];
    [self.bannerView addSubview:self.ccpView];
    
}

- (void)setValueForNoticeItemList:(NSMutableArray *)listArr {
    NSMutableArray *strArr = [NSMutableArray array];
    for (HomeNoticeItem *item in listArr) {
        [strArr addObject:item.ntitle];
    }
    self.ccpView.titleArray = strArr;
    self.ccpView.textFont = TextPingFangFontSize(12);
    self.ccpView.titleColor = [UIColor blackTextColor];
    
    __weak typeof(self) weakSelf = self;
    [self.ccpView clickTitleLabel:^(NSInteger index,NSString *titleString) {
        NSLog(@"%ld-----%@",index,titleString);
        if (weakSelf.noticeBlock) {
            weakSelf.noticeBlock(index);
        }
    }];
}

- (void)setValueForViewWithArr:(NSArray *)arr {
    [self.scrollView removeAllSubviews];
    
    CGFloat width = ScreenWidth * 0.25;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        
        NSInteger row = i / 4;
        NSInteger index = i % 4;
        
        HomeClassifyModel *model = [arr objectAtIndex:i];
        HomeClassifyItemView *itemView =[[HomeClassifyItemView alloc] initWithFrame:CGRectMake(width * index, 82 * row, width, 82)];
        itemView.titleLabel.text = model.name;
        itemView.imageView.image = [UIImage imageNamed:model.image];
        __weak typeof(self) weakSelf = self;
        itemView.block = ^{
            if (weakSelf.block) {
                weakSelf.block(i);
            }
        };
        [self.scrollView addSubview:itemView];
    }
}

#pragma mark - Getters
- (UIView *)bannerView {
    if (!_bannerView) {
        _bannerView = [[UIView alloc] init];
        _bannerView.backgroundColor = [UIColor whiteColor];
        _bannerView.layer.cornerRadius = 15;
        _bannerView.clipsToBounds = YES;
    }
    return _bannerView;
}

- (UIImageView *)newImageView {
    if (!_newImageView) {
        _newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notice"]];
        _newImageView.size = CGSizeMake(28, 16);
    }
    return _newImageView;
}

- (UILabel *)newLabel {
    if (!_newLabel) {
        _newLabel = [[UILabel alloc] init];
        _newLabel.font = TextPingFangMediumFontSize(16);
        _newLabel.textColor = [UIColor blackTextColor];
        _newLabel.text = @"公告";
    }
    return _newLabel;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor grayF9Color];
    }
    return _scrollView;
}

@end
